<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
$ui = new ui();
$tpl = new Template2();
$fieldArr = array("method" => "5", "gametype" => "0", "round" => "0", "lotto_id" => "0", "betcontent" => "", "orate" => "", "nrate" => "", "pagename" => "2", "paperopen" => date("Y-m-d"));
$tpl->load("main", "srate1.htm");
$tpl->set("cchinayear", intval($conf->get("chinayear")));
$disk = "a";
if (isset($_GET['disk']))
{
    $disk = $_GET['disk'];
}
else if (isset($_POST['disk']))
{
    $disk = $_POST['disk'];
}
$diskArr=array('a'=>1,'b'=>2,'c'=>3,'d'=>4);
$diskInt=$diskArr[$disk];

include( "admin_menu.php" );
include( "rate_menu.php" );
srateMenu("srate1", $disk);
$tpl->set("action", "op.php?op=admin&fp=srate1&disk=" . $disk);
$conf = new Config();
$rate0a_b = floatval($conf->get("rate0a_b"));
$rate0a_c = floatval($conf->get("rate0a_c"));
$rate0a_d = floatval($conf->get("rate0a_d"));
$rate1a_b = floatval($conf->get("rate1a_b"));
$rate1a_c = floatval($conf->get("rate1a_c"));
$rate1a_d = floatval($conf->get("rate1a_d"));
if ($disk == "a")
{
    $setRateFlag = true;
}
else
{
    $setRateFlag = false;
}
if (ispost())
{
    $fields = array();
    if ($setRateFlag)
    {
        $sqlRate = "!!SELECT * FROM rate0a WHERE lotto_id = 0 LIMIT 0,1";
        $rsRate = getdb($sqlRate);
    }
    $i = 1;
    while ($i < 50)
    {
        $ii = d2($i);
        $temp = floatval($_POST["r0_" . $ii]);
        $idx = "r" . $ii;
        $fields[$idx] = $temp;
        if ($setRateFlag && !$rsRate->eof && $rsRate->f[$idx] != $temp)
        {
            $fieldArr['betcontent'] = GetBetContent(0, $ii);
            $fieldArr['orate'] = $rsRate->f[$idx];
            $fieldArr['nrate'] = $temp;
            setRateRecord($fieldArr);
        }
        ++$i;
    }
    $sql = "update rate0{$disk} set ";
    foreach ($fields as $key => $temp)
    {
        $sql .= " {$key} = {$temp}, ";
    }
    if (substr($sql, 0 - 2) == ", ")
    {
        $sql = substr($sql, 0, strlen($sql) - 2);
    }
    $sql .= " WHERE lotto_id=0";
    getdb($sql);
    if ($disk == "a")
    {
        if ($rate0a_b != 0)
        {
            $fields = array();
            $i = 1;
            while ($i < 50)
            {
                $ii = d2($i);
                $fields["r" . $ii] = floatval($_POST["r0_" . $ii]) - $rate0a_b;
                ++$i;
            }
            $sql = "update rate0b set ";
            foreach ($fields as $key => $temp)
            {
                $sql .= " {$key} = {$temp}, ";
            }
            if (substr($sql, 0 - 2) == ", ")
            {
                $sql = substr($sql, 0, strlen($sql) - 2);
            }
            $sql .= " WHERE lotto_id=0";
            getdb($sql);
        }
        if ($rate0a_c != 0)
        {
            $fields = array();
            $i = 1;
            while ($i < 50)
            {
                $ii = d2($i);
                $fields["r" . $ii] = floatval($_POST["r0_" . $ii]) - $rate0a_c;
                ++$i;
            }
            $sql = "update rate0c set ";
            foreach ($fields as $key => $temp)
            {
                $sql .= " {$key} = {$temp}, ";
            }
            if (substr($sql, 0 - 2) == ", ")
            {
                $sql = substr($sql, 0, strlen($sql) - 2);
            }
            $sql .= " WHERE lotto_id=0";
            getdb($sql);
        }
        if ($rate0a_d != 0)
        {
            $fields = array();
            $i = 1;
            while ($i < 50)
            {
                $ii = d2($i);
                $fields["r" . $ii] = floatval($_POST["r0_" . $ii]) - $rate0a_d;
                ++$i;
            }
            $sql = "update rate0d set ";
            foreach ($fields as $key => $temp)
            {
                $sql .= " {$key} = {$temp}, ";
            }
            if (substr($sql, 0 - 2) == ", ")
            {
                $sql = substr($sql, 0, strlen($sql) - 2);
            }
            $sql .= " WHERE lotto_id=0";
            getdb($sql);
        }
    }
}
$rs = getdb("!!select * from rate0" . $disk . " where lotto_id=0");
if ($rs->eof)
{
    getdb("insert into rate0" . $disk . " (lotto_id) values (0)");
    $rs = getdb("!!select * from rate0" . $disk . " where lotto_id=0");
}
$i = 1;
while ($i < 50)
{
    $ii = d2($i);
    $tpl->set("r0_" . $ii, trimfloat($rs->f["r" . $ii], 1));
    ++$i;
}
$rs->close();

if (ispost())
{
    $fields = array();
    if (floatval($_POST['r1_1']) < 10)
    {
        $fields['r1_1'] = floatval($_POST['r1_1']);
    }
    if (floatval($_POST['r1_2']) < 10)
    {
        $fields['r1_2'] = floatval($_POST['r1_2']);
    }
    if (floatval($_POST['r2_1']) < 10)
    {
        $fields['r2_1'] = floatval($_POST['r2_1']);
    }
    if (floatval($_POST['r2_2']) < 10)
    {
        $fields['r2_2'] = floatval($_POST['r2_2']);
    }
    if (floatval($_POST['r3_1']) < 10)
    {
        $fields['r3_1'] = floatval($_POST['r3_1']);
    }
    if (floatval($_POST['r3_2']) < 10)
    {
        $fields['r3_2'] = floatval($_POST['r3_2']);
    }
    if (floatval($_POST['r20_1']) < 10)
    {
        $fields['r20_1'] = floatval($_POST['r20_1']);
    }
    if (floatval($_POST['r20_2']) < 10)
    {
        $fields['r20_2'] = floatval($_POST['r20_2']);
    }
    if (floatval($_POST['r13_1']) < 10)
    {
        $fields['r13_1'] = floatval($_POST['r13_1']);
    }
    if (floatval($_POST['r13_2']) < 10)
    {
        $fields['r13_2'] = floatval($_POST['r13_2']);
    }
    if (floatval($_POST['r13_3']) < 10)
    {
        $fields['r13_3'] = floatval($_POST['r13_3']);
    }
    $i = 1;
    while ($i <= 12)
    {
        $temp = floatval($_POST["r14_" . $i]);
        if ($temp < 20)
        {
            $fields["r14_" . $i] = $temp;
        }
        $temp = floatval($_POST["r18_" . $i]);
        if ($temp < 10)
        {
            $fields["r18_" . $i] = $temp;
        }
        ++$i;
    }
    $i = 1;
    while ($i <= 11)
    {
        if (floatval($_POST["r19_1_" . d2($i)]) < 20)
        {
            $fields["r19_1_" . d2($i)] = floatval($_POST["r19_1_" . d2($i)]);
        }
        if (floatval($_POST["r19_2_" . d2($i)]) < 20)
        {
            $fields["r19_2_" . d2($i)] = floatval($_POST["r19_2_" . d2($i)]);
        }
        ++$i;
    }
    if (floatval($_POST['r28_1']) < 10)
    {
        $fields['r28_1'] = floatval($_POST['r28_1']);
    }
    if (floatval($_POST['r28_2']) < 10)
    {
        $fields['r28_2'] = floatval($_POST['r28_2']);
    }
    if (floatval($_POST['r30_1']) < 10)
    {
        $fields['r30_1'] = floatval($_POST['r30_1']);
    }
    if (floatval($_POST['r30_2']) < 10)
    {
        $fields['r30_2'] = floatval($_POST['r30_2']);
    }
    if (floatval($_POST['r47_1']) < 10)
    {
        $fields['r47_1'] = floatval($_POST['r47_1']);
    }
    if (floatval($_POST['r47_2']) < 10)
    {
        $fields['r47_2'] = floatval($_POST['r47_2']);
    }
    if ($setRateFlag)
    {
        $sqlRate = "!!SELECT * FROM rate1a WHERE lotto_id = 0 LIMIT 0,1";
        $rsRate = getdb($sqlRate);
    }
    $sql = "update rate1{$disk} set ";
    foreach ($fields as $key => $temp)
    {
        $sql .= " {$key} = {$temp}, ";
        if ($setRateFlag && !$rsRate->eof && $rsRate->f[$key] != $temp)
        {
            $fieldArr['betcontent'] = GetBetContentForSetRateRecord($key);
            $fieldArr['orate'] = $rsRate->f[$key];
            $fieldArr['nrate'] = $temp;
            setRateRecord($fieldArr);
        }
    }
    if (substr($sql, 0 - 2) == ", ")
    {
        $sql = substr($sql, 0, strlen($sql) - 2);
    }
    $sql .= " WHERE lotto_id=0";
    getdb($sql);
	//特码头尾
	$fields=array();
	for($i=0;$i<=4;$i++)
	{
		if (floatval($_POST['r49_0_'.$i]) < 10)
		{
			$fields['r'.$i] = floatval($_POST['r49_'.$i]);
		}	
	}
	$sql = "update rate37 set ";
    foreach ($fields as $key => $temp)
    {
        $sql .= " {$key} = {$temp}, ";
        if ($setRateFlag && !$rsRate->eof && $rsRate->f[$key] != $temp)
        {
            $fieldArr['betcontent'] = GetBetContentForSetRateRecord($key);
            $fieldArr['orate'] = $rsRate->f[$key];
            $fieldArr['nrate'] = $temp;
            setRateRecord($fieldArr);
        }
    }
    if (substr($sql, 0 - 2) == ", ")
    {
        $sql = substr($sql, 0, strlen($sql) - 2);
    }
    $sql .= " WHERE lotto_id=0 and bettype=49 and disk=".$diskInt;
    getdb($sql);
	$fields=array();
	for($i=0;$i<=9;$i++)
	{
		if (floatval($_POST['r50_0_'.$i]) < 10)
		{
			$fields['r'.$i] = floatval($_POST['r50_'.$i]);
		}	
	}
	$sql = "update rate37 set ";
    foreach ($fields as $key => $temp)
    {
        $sql .= " {$key} = {$temp}, ";
        if ($setRateFlag && !$rsRate->eof && $rsRate->f[$key] != $temp)
        {
            $fieldArr['betcontent'] = GetBetContentForSetRateRecord($key);
            $fieldArr['orate'] = $rsRate->f[$key];
            $fieldArr['nrate'] = $temp;
            setRateRecord($fieldArr);
        }
    }
    if (substr($sql, 0 - 2) == ", ")
    {
        $sql = substr($sql, 0, strlen($sql) - 2);
    }
    $sql .= " WHERE lotto_id=0 and bettype=50 and disk=".$diskInt;
    getdb($sql);
    if ($disk == "a" && $rate1a_b != 0)
    {
        $fields = array();
        $fields['r1_1'] = floatval($_POST['r1_1']) - $rate1a_b;
        $fields['r1_2'] = floatval($_POST['r1_2']) - $rate1a_b;
        $fields['r2_1'] = floatval($_POST['r2_1']) - $rate1a_b;
        $fields['r2_2'] = floatval($_POST['r2_2']) - $rate1a_b;
        $fields['r3_1'] = floatval($_POST['r3_1']) - $rate1a_b;
        $fields['r3_2'] = floatval($_POST['r3_2']) - $rate1a_b;
        $fields['r20_1'] = floatval($_POST['r20_1']) - $rate1a_b;
        $fields['r20_2'] = floatval($_POST['r20_2']) - $rate1a_b;
        $fields['r13_1'] = floatval($_POST['r13_1']);
        $fields['r13_2'] = floatval($_POST['r13_2']);
        $fields['r13_3'] = floatval($_POST['r13_3']);
        $i = 1;
        while ($i <= 12)
        {
            $fields["r14_" . $i] = max(0, floatval($_POST["r14_" . $i]));
            $fields["r18_" . $i] = max(0, floatval($_POST["r18_" . $i]));
            ++$i;
        }
        $i = 1;
        while ($i <= 11)
        {
            $fields["r19_1_" . d2($i)] = max(0, floatval($_POST["r19_1_" . d2($i)]) - $rate1a_b);
            $fields["r19_2_" . d2($i)] = max(0, floatval($_POST["r19_2_" . d2($i)]) - $rate1a_b);
            ++$i;
        }
        $fields['r28_1'] = max(0, floatval($_POST['r28_1']) - $rate1a_b);
        $fields['r28_2'] = max(0, floatval($_POST['r28_2']) - $rate1a_b);
        $fields['r30_1'] = max(0, floatval($_POST['r30_1']) - $rate1a_b);
        $fields['r30_2'] = max(0, floatval($_POST['r30_2']) - $rate1a_b);
        $fields['r47_1'] = max(0, floatval($_POST['r47_1']) - $rate1a_b);
        $fields['r47_2'] = max(0, floatval($_POST['r47_2']) - $rate1a_b);
        $sql = "update rate1b set ";
        foreach ($fields as $key => $temp)
        {
            $sql .= " {$key} = {$temp}, ";
        }
        if (substr($sql, 0 - 2) == ", ")
        {
            $sql = substr($sql, 0, strlen($sql) - 2);
        }
        $sql .= " WHERE lotto_id=0";
        getdb($sql);
    }
    if ($disk == "a" && $rate1a_c != 0)
    {
        $fields = array();
        $fields['r1_1'] = floatval($_POST['r1_1']) - $rate1a_c;
        $fields['r1_2'] = floatval($_POST['r1_2']) - $rate1a_c;
        $fields['r2_1'] = floatval($_POST['r2_1']) - $rate1a_c;
        $fields['r2_2'] = floatval($_POST['r2_2']) - $rate1a_c;
        $fields['r3_1'] = floatval($_POST['r3_1']) - $rate1a_c;
        $fields['r3_2'] = floatval($_POST['r3_2']) - $rate1a_c;
        $fields['r20_1'] = floatval($_POST['r20_1']) - $rate1a_c;
        $fields['r20_2'] = floatval($_POST['r20_2']) - $rate1a_c;
        $fields['r20_2'] = floatval($_POST['r20_2']) - $rate1a_c;
        $fields['r13_1'] = floatval($_POST['r13_1']);
        $fields['r13_2'] = floatval($_POST['r13_2']);
        $fields['r13_3'] = floatval($_POST['r13_3']);
        $i = 1;
        while ($i <= 12)
        {
            $fields["r14_" . $i] = max(0, floatval($_POST["r14_" . $i]));
            $fields["r18_" . $i] = max(0, floatval($_POST["r18_" . $i]));
            ++$i;
        }
        $i = 1;
        while ($i <= 11)
        {
            $fields["r19_1_" . d2($i)] = max(0, floatval($_POST["r19_1_" . d2($i)]) - $rate1a_c);
            $fields["r19_2_" . d2($i)] = max(0, floatval($_POST["r19_2_" . d2($i)]) - $rate1a_c);
            ++$i;
        }
        $fields['r28_1'] = max(0, floatval($_POST['r28_1']) - $rate1a_c);
        $fields['r28_2'] = max(0, floatval($_POST['r28_2']) - $rate1a_c);
        $fields['r30_1'] = max(0, floatval($_POST['r30_1']) - $rate1a_c);
        $fields['r30_2'] = max(0, floatval($_POST['r30_2']) - $rate1a_c);
        $fields['r47_1'] = max(0, floatval($_POST['r47_1']) - $rate1a_c);
        $fields['r47_2'] = max(0, floatval($_POST['r47_2']) - $rate1a_c);
        $sql = "update rate1c set ";
        foreach ($fields as $key => $temp)
        {
            $sql .= " {$key} = {$temp}, ";
        }
        if (substr($sql, 0 - 2) == ", ")
        {
            $sql = substr($sql, 0, strlen($sql) - 2);
        }
        $sql .= " WHERE lotto_id=0";
        getdb($sql);
    }
    if ($disk == "a" && $rate1a_d != 0)
    {
        $fields = array();
        $fields['r1_1'] = floatval($_POST['r1_1']) - $rate1a_d;
        $fields['r1_2'] = floatval($_POST['r1_2']) - $rate1a_d;
        $fields['r2_1'] = floatval($_POST['r2_1']) - $rate1a_d;
        $fields['r2_2'] = floatval($_POST['r2_2']) - $rate1a_d;
        $fields['r3_1'] = floatval($_POST['r3_1']) - $rate1a_d;
        $fields['r3_2'] = floatval($_POST['r3_2']) - $rate1a_d;
        $fields['r20_1'] = floatval($_POST['r20_1']) - $rate1a_d;
        $fields['r20_2'] = floatval($_POST['r20_2']) - $rate1a_d;
        $fields['r13_1'] = floatval($_POST['r13_1']);
        $fields['r13_2'] = floatval($_POST['r13_2']);
        $fields['r13_3'] = floatval($_POST['r13_3']);
        $i = 1;
        while ($i <= 12)
        {
            $fields["r14_" . $i] = max(0, floatval($_POST["r14_" . $i]));
            $fields["r18_" . $i] = max(0, floatval($_POST["r18_" . $i]));
            ++$i;
        }
        $i = 1;
        while ($i <= 11)
        {
            $fields["r19_1_" . d2($i)] = max(0, floatval($_POST["r19_1_" . d2($i)]) - $rate1a_d);
            $fields["r19_2_" . d2($i)] = max(0, floatval($_POST["r19_2_" . d2($i)]) - $rate1a_d);
            ++$i;
        }
        $fields['r28_1'] = max(0, floatval($_POST['r28_1']) - $rate1a_d);
        $fields['r28_2'] = max(0, floatval($_POST['r28_2']) - $rate1a_d);
        $fields['r30_1'] = max(0, floatval($_POST['r30_1']) - $rate1a_d);
        $fields['r30_2'] = max(0, floatval($_POST['r30_2']) - $rate1a_d);
        $fields['r47_1'] = max(0, floatval($_POST['r47_1']) - $rate1a_d);
        $fields['r47_2'] = max(0, floatval($_POST['r47_2']) - $rate1a_d);
        $sql = "update rate1d set ";
        foreach ($fields as $key => $temp)
        {
            $sql .= " {$key} = {$temp}, ";
        }
        if (substr($sql, 0 - 2) == ", ")
        {
            $sql = substr($sql, 0, strlen($sql) - 2);
        }
        $sql .= " WHERE lotto_id=0";
        getdb($sql);
    }
}
$rs = getdb("!!select * from rate1" . $disk . " where lotto_id=0");
if ($rs->eof)
{
    getdb("insert into rate1" . $disk . " (lotto_id) values (0)");
    $rs = getdb("!!select * from rate1" . $disk . " where lotto_id=0");
}
$tpl->set("r1_1", trimfloat($rs->f['r1_1'], 1));
$tpl->set("r1_2", trimfloat($rs->f['r1_2'], 1));
$tpl->set("r2_1", trimfloat($rs->f['r2_1'], 1));
$tpl->set("r2_2", trimfloat($rs->f['r2_2'], 1));
$tpl->set("r3_1", trimfloat($rs->f['r3_1'], 1));
$tpl->set("r3_2", trimfloat($rs->f['r3_2'], 1));
$tpl->set("r20_1", trimfloat($rs->f['r20_1'], 1));
$tpl->set("r20_2", trimfloat($rs->f['r20_2'], 1));
$tpl->set("r13_1", trimfloat($rs->f['r13_1'], 1));
$tpl->set("r13_2", trimfloat($rs->f['r13_2'], 1));
$tpl->set("r13_3", trimfloat($rs->f['r13_3'], 1));
$i = 1;
while ($i <= 12)
{
    $tpl->set("r14_" . $i, trimfloat($rs->f["r14_" . $i], 1));
    $tpl->set("r18_" . $i, trimfloat($rs->f["r18_" . $i], 1));
    ++$i;
}
$i = 1;
while ($i <= 11)
{
    $tpl->set("r19_1_" . d2($i), trimfloat($rs->f["r19_1_" . d2($i)], 1));
    $tpl->set("r19_2_" . d2($i), trimfloat($rs->f["r19_2_" . d2($i)], 1));
    ++$i;
}
$tpl->set("r28_1", trimfloat($rs->f['r28_1'], 1));
$tpl->set("r28_2", trimfloat($rs->f['r28_2'], 1));
$tpl->set("r30_1", trimfloat($rs->f['r30_1'], 1));
$tpl->set("r30_2", trimfloat($rs->f['r30_2'], 1));
$tpl->set("r47_1", trimfloat($rs->f['r47_1'], 1));
$tpl->set("r47_2", trimfloat($rs->f['r47_2'], 1));
//特码头尾
$htm="";
$rs = getdb("!!select * from rate37 where lotto_id=0 and bettype=49 and disk=".$diskInt);
if(!$rs->eof)
{
	$htm = "<table border='0' cellspacing='0' cellpadding='3'><tr><tr><td colspan='2' bgcolor='#5A79C6' align='center'><font color='#FFFFFF'>特码尾</font></td></tr><td valign='top'>"."<table cellSpacing='1' cellPadding='3' border='0'><tr>";
	$i = 0;
	while ( $i <= 4 )
	{
		$val = trimfloat( $rs->f["r".$i], 1 );
		$htm .= "<td bgColor='#c0d6eb'><div align='center'>头{$i}</td></div><td bgColor='#e7ebef'>"."<input size='5' name=\"r49_{$i}\" value='{$val}'>"."<input type='button' value='-' onclick=\"m1(r49_{$i},-0.01)\">"."<input type='button' value='+' onclick=m1(r49_{$i},0.01)></td>";
		++$i;
	}
	$htm .= "</tr>";
	$htm .= "</table></td><td valign=\"top\"></td></tr></table>";
	$htm .= "<br>";
}
$rs->close();
$rs = getdb("!!select * from rate37 where lotto_id=0 and bettype=50 and disk=".$diskInt);
if(!$rs->eof)
{
	$htm .= "<table border='0' cellspacing='0' cellpadding='3'><tr><tr><td colspan='2' bgcolor='#5A79C6' align='center'><font color='#FFFFFF'>特码尾</font></td></tr><td valign='top'>"."<table cellSpacing='1' cellPadding='3' border='0'><tr>";
	$i = 0;
	while ( $i <= 4 )
	{
		$val = trimfloat( $rs->f["r".$i], 1 );
		$htm .= "<td bgColor='#c0d6eb'><div align='center'>尾{$i}</td></div><td bgColor='#e7ebef'>"."<input size='5' name=\"r50_{$i}\" value='{$val}'>"."<input type='button' value='-' onclick=\"m1(r50_{$i},-0.01)\">"."<input type='button' value='+' onclick=m1(r50_{$i},0.01)></td>";
		++$i;
	}
	$htm .= "</tr><tr>";
	$i = 5;
	while ( $i <= 9 )
	{
		$val = trimfloat( $rs->f["r".$i], 1 );
		$htm .= "<td bgColor='#c0d6eb'><div align='center'>尾{$i}</td></div><td bgColor='#e7ebef'>"."<input size='5' name=\"r50_{$i}\" value='{$val}'>"."<input type='button' value='-' onclick=\"m1(r50_{$i},-0.01)\">"."<input type='button' value='+' onclick=m1(r50_{$i},0.01)></td>";
		++$i;
	}
	$htm .= "</table></td><td valign=\"top\"></td></tr></table>";
}
$rs->close();
$tpl->set("htm",$htm);
$tpl->show("main");
?>